### variables ###

#
# project stats
#
PROJECT_NAME=avdl-cengine
PROJECT_VERSION=1.0.0

#
# directories
#
#
# SRC: source files directory
# BUILD: compiled build directory (everything compiled belongs here)
# OBJ: compiled objects directory
# DIRECTORIES: directories that should be created if they don't exist
#
DIRECTORY_SRC=src
DIRECTORY_BUILD=build
DIRECTORY_OBJ=${DIRECTORY_BUILD}/objects
DIRECTORIES=${DIRECTORY_BUILD} ${DIRECTORY_OBJ}

#
# source files
#
SRC=$(wildcard ${DIRECTORY_SRC}/*.c)
OBJ=${SRC:${DIRECTORY_SRC}/%.c=${DIRECTORY_OBJ}/%.o}
HEADERS=$(wildcard include/*.h)

#
# compiler flags
#
#COMPILER_FLAGS=-Wall -Werror -Wpedantic
COMPILER_FLAGS=-Wall -Wpedantic -Wformat-security

#
# output library
#
STATIC_OUT=${DIRECTORY_BUILD}/lib${PROJECT_NAME}.a

#
# install data
#
prefix=/usr/local

### recipes ###

#
# compile the whole library
#
all: ${DIRECTORIES} ${STATIC_OUT}

#
# create static library
#
${STATIC_OUT}: ${OBJ}
	ar rcs $@ $^

#
# make sure compiled build directories exist
#
${DIRECTORIES}:
	mkdir $@

#
# compile source files to object files
#
${DIRECTORY_OBJ}/%.o: ${DIRECTORY_SRC}/%.c ${HEADERS}
	${CC} ${COMPILER_FLAGS} -Iinclude -DDD_PLATFORM_NATIVE -o $@ -c $<

#
# install library
#
install: ${STATIC_OUT}
	mkdir -p ${DESTDIR}${prefix}/include
	install -m644 ${HEADERS} ${DESTDIR}${prefix}/include
	mkdir -p ${DESTDIR}${prefix}/lib
	install -m755 ${STATIC_OUT} ${DESTDIR}${prefix}/lib

#
# clean working directory
#
clean:
	rm -f ${OBJ} ${STATIC_OUT}

#
# phony recipes
#
.PHONY: clean
